package com.ruoyi.shaketicket.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.shaketicket.mapper.ShaketicketAwardMapper;
import com.ruoyi.shaketicket.mapper.ShaketicketConfigMapper;
import com.ruoyi.shaketicket.mapper.ShaketicketRecordMapper;
import com.ruoyi.shaketicket.domain.ShaketicketAward;
import com.ruoyi.shaketicket.domain.ShaketicketRecord;
import com.ruoyi.shaketicket.service.IShaketicketRecordService;
import com.ruoyi.common.support.Convert;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.UUIDUtil;
import com.ruoyi.jiugongge.domain.WeixinDto;

/**
 * 摇一摇抽奖记录 服务层实现
 * 
 * @author lzy
 * @date 2019-03-01
 */
@Service
public class ShaketicketRecordServiceImpl implements IShaketicketRecordService 
{
	@Autowired
	private ShaketicketRecordMapper shaketicketRecordMapper;
	@Autowired
	private ShaketicketConfigMapper shaketicketConfigMapper;
	@Autowired
	private ShaketicketAwardMapper shaketicketAwardMapper;
	

	/**
     * 查询摇一摇抽奖记录信息
     * 
     * @param id 摇一摇抽奖记录ID
     * @return 摇一摇抽奖记录信息
     */
    @Override
	public ShaketicketRecord selectShaketicketRecordById(String id)
	{
	    return shaketicketRecordMapper.selectShaketicketRecordById(id);
	}
	
	/**
     * 查询摇一摇抽奖记录列表
     * 
     * @param shaketicketRecord 摇一摇抽奖记录信息
     * @return 摇一摇抽奖记录集合
     */
	@Override
	public List<ShaketicketRecord> selectShaketicketRecordList(ShaketicketRecord shaketicketRecord)
	{
	    return shaketicketRecordMapper.selectShaketicketRecordList(shaketicketRecord);
	}
	
    /**
     * 新增摇一摇抽奖记录
     * 
     * @param shaketicketRecord 摇一摇抽奖记录信息
     * @return 结果
     */
	@Override
	public int insertShaketicketRecord(ShaketicketRecord shaketicketRecord)
	{
	    return shaketicketRecordMapper.insertShaketicketRecord(shaketicketRecord);
	}
	
	/**
     * 修改摇一摇抽奖记录
     * 
     * @param shaketicketRecord 摇一摇抽奖记录信息
     * @return 结果
     */
	@Override
	public int updateShaketicketRecord(ShaketicketRecord shaketicketRecord)
	{
	    return shaketicketRecordMapper.updateShaketicketRecord(shaketicketRecord);
	}

	/**
     * 删除摇一摇抽奖记录对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteShaketicketRecordByIds(String ids)
	{
		return shaketicketRecordMapper.deleteShaketicketRecordByIds(Convert.toStrArray(ids));
	}

	@Override
	public Map<String, Integer> getRecordCountByActIdAndOpenid(String actId, String openid, Date currDate) {
		// TODO Auto-generated method stub
		return shaketicketRecordMapper.getRecordCountByActIdAndOpenid(actId,openid,currDate);
	}

	@Override
    public ShaketicketAward creatRecordAndReturnAward(
				ShaketicketRecord shaketicketRecord, WeixinDto weixinDto) {
			shaketicketRecord.setActId(weixinDto.getActId());
			shaketicketRecord.setOpenid(weixinDto.getOpenid());
			shaketicketRecord.setJwid(weixinDto.getJwid());
			shaketicketRecord.setDrawTime(new Date());//记录抽奖时间
			shaketicketRecord.setReceiveStatus("0");//初始化为未领奖
			//获取中奖序号
			Integer maxAwardsSeq = shaketicketRecordMapper.getMaxSeq(shaketicketRecord.getActId());
			Integer nextAwardsSeq = maxAwardsSeq+1;
			shaketicketRecord.setAwardsSeq(nextAwardsSeq);
			shaketicketRecord.setId(UUIDUtil.creatUUID());
			shaketicketRecordMapper.insertShaketicketRecord(shaketicketRecord);
			 if(StringUtils.isEmpty(shaketicketRecord.getAwardId())){
				 return new ShaketicketAward();			 
			 }else{
				 shaketicketConfigMapper.updateRemainNum(weixinDto.getActId(), weixinDto.getJwid(), shaketicketRecord.getAwardId());
				 return  shaketicketAwardMapper.selectShaketicketAwardById(shaketicketRecord.getAwardId());
			 }
		}
	
	
}
